package com.bff.gaia.mix.api.xjoin2.partition;

import com.bff.gaia.api.java.functions.KeySelector;
import com.bff.gaia.mix.api.xjoin2.core.table.XjoinInfo;
import com.bff.gaia.types.Row;
import com.bff.gaia.util.MathUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.bff.gaia.mix.api.xjoin2.core.utils.KeyUtil.buildKey;

public class StreamKeySelector implements KeySelector<Row, Integer> {
	private final int[] streamTableKeyPos;
	private final int parallelism;

	public StreamKeySelector(int parallelism, int... streamTableKeyPos) {
		this.parallelism = parallelism;
		this.streamTableKeyPos = streamTableKeyPos;
	}

	@Override
	public Integer getKey(Row value) throws Exception {
		return MathUtils.murmurHash(buildKey(value, streamTableKeyPos).hashCode()) %
			parallelism;
	}
}